<template>
	<view class="bg-white padding height-100">
		<view class="text-xl text-bold">请输入密码</view>
		<view class="row">
			<u--image width="30rpx" height="40rpx" mode="aspectFit" :src="codeImage"></u--image>
			<view class="margin-left">
				<u--input v-model="password" type="password" fontSize="34rpx" :border="false" placeholder="输入密码"></u--input>
			</view>
		</view>
		<view class="margin-top text-xl text-bold">请输入密码</view>
		<view class="row">
			<u--image width="30rpx" height="40rpx" mode="aspectFit" :src="codeImage"></u--image>
			<view class="margin-left">
				<u--input v-model="newpassword" type="password" fontSize="34rpx" :border="false" placeholder="输入密码"></u--input>
			</view>
		</view>
		<view class="margin-top-xxl flex justify-center">
			<view class="login-btn pwd-btn common-btn" @tap="save">去登录</view>
		</view>
	</view>
</template>

<script>
	import codeImage from "@/static/login/icon-code.png"
	import {
		retrieve
	} from "@/api/Interface/login"
	import {
		VerCodeTypeEnum
	} from "@/enums/verCode"
    import JSEncryptUtils from '@/utils/JSEncrypt.js';
	export default {
		data() {
			return {
				codeImage,
				password: "",
				newpassword: "",
			}
		},
		props: {
			phone: {
				type: String,
				default: ""
			},
			codeType: {
				type: Number,
				default: VerCodeTypeEnum['忘记密码']
			},
		},
		methods: {
			save() {
				if (uni.$u.test.empty(this.password)) {
					this.$toast("请输入密码")
					return
				}

				if (uni.$u.test.empty(this.newpassword)) {
					this.$toast("请输入确认密码")
					return
				}
				if (this.password !== this.newpassword) {
					this.$toast("两次密码输入不一致")
					return
				}

				retrieve({
					phonenumber:JSEncryptUtils.encrypt(this.phone),
					username: this.phone,
					password: JSEncryptUtils.encrypt(this.password),
					newpassword: JSEncryptUtils.encrypt(this.newpassword),
					flag: this.codeType
				}).then(res => {
					this.$toast(res.msg)
					if (res.code === 200) {
						this.$emit("save", res.code === 200)
					}
				})
			}
		},
	}
</script>

<style lang="less" scoped>
	@import url("@/pages/login/css/login.css");

	.pwd-btn {
		width: 50%;
	}
</style>